package com.gaofei.dofile.controller;

import com.bw.itext.pdf.service.PdfService;
import com.bw.springboot.starter.upload.model.Chunk;
import com.bw.springboot.starter.upload.service.FileService;
import com.bw.springboot.starter.upload.util.FileResult;
import com.gaofei.dofile.domain.Report;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("file")
public class FileController {
    @Autowired
    private FileService fileService;


    @Autowired
    private PdfService pdfService;


    @RequestMapping("pdf")
    public String pdf () throws IOException {
        Report report1 = new Report(1, "体检报告", 3232323L);
        Report report2 = new Report(2, "审查报告", 3232323L);
        Report report3 = new Report(3, "年度报告", 3232323L);
        List<Report> reports = Arrays.asList(report1, report2, report3); //使用Arrays.asList方法产生的集合,不能进行增,删操作!!!!!
        pdfService.export(reports,"report.pdf","报告总结",Report.class);  //生成pdf文件

        pdfService.sign("report.pdf","report.sign.pdf"); //对文件进行签章

        return "success";
    }

    @RequestMapping("bigupload")
    public String bigupload(@ModelAttribute Chunk chunk) throws InterruptedException {
        fileService.postFileUpload(chunk);
        if (chunk.getTotalChunks().equals(chunk.getChunkNumber())) {
            TimeUnit.SECONDS.sleep(2);
            FileResult fileResult = fileService.mergeFile(chunk);
            return "文件合并";
        }
        return "success big!!!!";
    }
}
